import 'dart:io';

import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:pornhub_app/components/app_bg_view.dart';
import 'package:pornhub_app/components/easy_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/generate/app_image_path.dart';
import 'package:pornhub_app/views/mine/frontpage/controller/change_desktop_controller.dart';

import '../../../assets/styles.dart';
import '../../../utils/color.dart';
import 'change_icon_pop.dart';

/// 设置桌面图标
class SetPrivacyIconPage extends StatelessWidget {
  const SetPrivacyIconPage({super.key});

  @override
  Widget build(BuildContext context) {
    final bgColor = Styles.color.bgColor;

    return Scaffold(
      backgroundColor: bgColor,
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        iconTheme: const IconThemeData(color: Colors.white),
        title: Text("设置桌面隐私图标",
            textAlign: TextAlign.center,
            style: TextStyle(
              color: COLOR.hexColor('#ffffff'),
              fontSize: 16.sp,
              fontWeight: FontWeight.w500,
            )),
        centerTitle: true,
      ),
      body: GetBuilder<ChangeDesktopController>(
          init: ChangeDesktopController(),
          builder: (topController) {
            return ChangeDesktopChild(topController);
          }),
    );
  }
}

class ChangeDesktopChild extends StatefulWidget {
  final ChangeDesktopController topController;

  const ChangeDesktopChild(this.topController, {super.key});

  @override
  State<StatefulWidget> createState() => ChangeDesktopChildState();
}

class ChangeDesktopChildState extends State<ChangeDesktopChild> {
  void onClick(int index) {
    setState(() {
      widget.topController.index.value = index;
    });
  }

  void onSaveClick() {
    if (widget.topController.index < 1) {
      EasyToast.show('请选择图标');
      return;
    }
    SmartDialog.showLoading(
      msg: "",
      animationBuilder: (controller, child, animationParam) {
        return ChangeIconPop(
          tap: () {
            widget.topController.changeIcon(widget.topController.index.value);
            SmartDialog.dismiss();
          },
        );
      },
      alignment: Alignment.center,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        padding:
            EdgeInsets.only(left: 10.w, right: 10.w, top: 10.w, bottom: 30.w),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Column(
              children: [
                InkWell(
                  onTap: () => onClick(1),
                  child: Stack(
                    children: [
                      Image.asset(
                        'assets/mine/img_change_desktop_one.png',
                        width: 72.w,
                        height: 72.w,
                      ),
                      Visibility(
                        visible: widget.topController.index.value == 1,
                        child: Image.asset(
                          'assets/mine/img_change_icon_border.png',
                          width: 72.w,
                          height: 72.w,
                        ),
                      ),
                    ],
                  ),
                ),
                Text(
                  "计算器",
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: COLOR.hexColor(widget.topController.index.value == 1
                        ? '#B940FF'
                        : '#ffffff'),
                  ),
                ),
              ],
            ),
            Padding(padding: EdgeInsets.only(top: 24.w)),
            Flex(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              direction: Axis.horizontal,
              children: [
                Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    InkWell(
                      onTap: () => onClick(2),
                      child: Stack(
                        children: [
                          Image.asset(
                            'assets/mine/img_change_desktop_two.png',
                            width: 72.w,
                            height: 72.w,
                          ),
                          Visibility(
                            visible: widget.topController.index.value == 2,
                            child: Image.asset(
                              'assets/mine/img_change_icon_border.png',
                              width: 72.w,
                              height: 72.w,
                            ),
                          ),
                        ],
                      ),
                    ),
                    Text(
                      "网易云",
                      style: TextStyle(
                        fontSize: 14.sp,
                        color: COLOR.hexColor(
                            widget.topController.index.value == 2
                                ? '#B940FF'
                                : '#ffffff'),
                      ),
                    ),
                  ],
                ),
                SizedBox(width: 70.w),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    InkWell(
                      onTap: () => onClick(3),
                      child: Stack(
                        children: [
                          Image.asset(
                            'assets/mine/img_change_desktop_three.png',
                            width: 72.w,
                            height: 72.w,
                          ),
                          Visibility(
                            visible: widget.topController.index.value == 3,
                            child: Image.asset(
                              'assets/mine/img_change_icon_border.png',
                              width: 72.w,
                              height: 72.w,
                            ),
                          ),
                        ],
                      ),
                    ),
                    Text(
                      "球球英雄",
                      style: TextStyle(
                        fontSize: 14.sp,
                        color: COLOR.hexColor(
                            widget.topController.index.value == 3
                                ? '#B940FF'
                                : '#ffffff'),
                      ),
                    ),
                  ],
                ),
              ],
            ),
            Padding(padding: EdgeInsets.only(top: 24.w)),
            Flex(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              direction: Axis.horizontal,
              children: [
                Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    InkWell(
                      onTap: () => onClick(4),
                      child: Stack(
                        children: [
                          Image.asset(
                            'assets/mine/img_change_desktop_four.png',
                            width: 72.w,
                            height: 72.w,
                          ),
                          Visibility(
                            visible: widget.topController.index.value == 4,
                            child: Image.asset(
                              'assets/mine/img_change_icon_border.png',
                              width: 72.w,
                              height: 72.w,
                            ),
                          ),
                        ],
                      ),
                    ),
                    Text(
                      "图片处理",
                      style: TextStyle(
                        fontSize: 14.sp,
                        color: COLOR.hexColor(
                            widget.topController.index.value == 4
                                ? '#B940FF'
                                : '#ffffff'),
                      ),
                    ),
                  ],
                ),
                SizedBox(width: 70.w),
                Column(
                  children: [
                    InkWell(
                      onTap: () => onClick(5),
                      child: Stack(
                        children: [
                          Image.asset(
                            'assets/mine/img_change_desktop_five.png',
                            width: 72.w,
                            height: 72.w,
                          ),
                          Visibility(
                            visible: widget.topController.index.value == 5,
                            child: Image.asset(
                              'assets/mine/img_change_icon_border.png',
                              width: 72.w,
                              height: 72.w,
                            ),
                          ),
                        ],
                      ),
                    ),
                    Text(
                      "第五人格",
                      style: TextStyle(
                        fontSize: 14.sp,
                        color: COLOR.hexColor(
                            widget.topController.index.value == 5
                                ? '#B940FF'
                                : '#ffffff'),
                      ),
                    ),
                  ],
                ),
              ],
            ),
            Padding(padding: EdgeInsets.only(top: 24.w)),
            Flex(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              direction: Axis.horizontal,
              children: [
                Column(
                  children: [
                    InkWell(
                      onTap: () => onClick(6),
                      child: Stack(
                        children: [
                          Image.asset(
                            'assets/mine/img_change_desktop_two.png',
                            width: 72.w,
                            height: 72.w,
                          ),
                          Visibility(
                            visible: widget.topController.index.value == 6,
                            child: Image.asset(
                              'assets/mine/img_change_icon_border.png',
                              width: 72.w,
                              height: 72.w,
                            ),
                          ),
                        ],
                      ),
                    ),
                    Text(
                      "网易云",
                      style: TextStyle(
                        fontSize: 14.sp,
                        color: COLOR.hexColor(
                            widget.topController.index.value == 6
                                ? '#B940FF'
                                : '#ffffff'),
                      ),
                    ),
                  ],
                ),
                SizedBox(width: 70.w),
                Column(
                  children: [
                    InkWell(
                      onTap: () => onClick(7),
                      child: Stack(
                        children: [
                          Image.asset(
                            'assets/mine/img_change_desktop_three.png',
                            width: 72.w,
                            height: 72.w,
                          ),
                          Visibility(
                            visible: widget.topController.index.value == 7,
                            child: Image.asset(
                              'assets/mine/img_change_icon_border.png',
                              width: 72.w,
                              height: 72.w,
                            ),
                          ),
                        ],
                      ),
                    ),
                    Text(
                      "球球英雄",
                      style: TextStyle(
                        fontSize: 14.sp,
                        color: COLOR.hexColor(
                            widget.topController.index.value == 7
                                ? '#B940FF'
                                : '#ffffff'),
                      ),
                    ),
                  ],
                ),
              ],
            ),
            Padding(padding: EdgeInsets.only(top: 24.w)),
            InkWell(
              onTap: () => onSaveClick(),
              child: AppBgView(
                width: double.maxFinite,
                margin: EdgeInsets.symmetric(horizontal: 22.w),
                height: 40.w,
                imagePath: AppImagePath.mine_img_exchange_vip_bg,
                fit: BoxFit.fill,
                child: Flex(
                  direction: Axis.horizontal,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      "确认选择",
                      style: TextStyle(
                        fontSize: 14.sp,
                        color: Colors.white,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ));
  }
}
